package com.cloudcompare.diff.compare;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class JavapWrapper
{
  static Method mMain = null;

  static File findFile(File paramFile, String paramString)
  {
    System.err.println("looking in " + paramFile);
    File localFile1 = new File(paramFile, paramString);
    if (localFile1.exists())
      return localFile1;
    File[] arrayOfFile = paramFile.listFiles(new FilenameFilter() {
      public boolean accept(File paramFile, String paramString) {
        return (paramString != ".") && (paramString != "..");
      }
    });
    for (int i = 0; i < arrayOfFile.length; i++) {
      File localFile2 = arrayOfFile[i];
      if (localFile2.isFile()) {
        continue;
      }
      File localFile3 = findFile(localFile2, paramString);
      if (localFile3 != null)
        return localFile3;
    }
    return null;
  }

  static boolean instantiate()
  {
    File localFile1 = new File(System.getProperty("java.home"));

    File localFile2 = new File(localFile1, "..");
    localFile2 = new File(localFile2, "lib");
    localFile2 = new File(localFile2, "tools.jar");
    try { localFile2 = new File(localFile2.getCanonicalPath()); } catch (Exception localException1) {
    }if ((localFile2 == null) || (!localFile2.exists())) {
      System.err.println(localFile2 + " does not exist!");
      System.err.println("Could not find tools.jar, this needs to be running under a JDK, not JRE");
      return false;
    }
    try {
      URL[] arrayOfURL = { localFile2.toURL() };

      URLClassLoader localURLClassLoader = new URLClassLoader(arrayOfURL);

      Class localClass = localURLClassLoader.loadClass("sun.tools.javap.Main");
      mMain = localClass.getDeclaredMethod("main", new Class[] { new String[0].getClass() });
    } catch (Exception localException2) {
      System.err.println("Could not load tools.jar, this needs to be running under a JDK, not JRE");
      localException2.printStackTrace();
      return false;
    }
    return true;
  }

  public static String run(String paramString1, String paramString2)
  {
    if (mMain == null) {
      return "Error";
    }
    String[] arrayOfString = { "-c", "-classpath", paramString2, paramString1 };
    try
    {
      ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();

      PrintStream localPrintStream1 = System.out;
      PrintStream localPrintStream2 = new PrintStream(localByteArrayOutputStream);

      System.setOut(localPrintStream2);

      mMain.invoke(null, new Object[] { arrayOfString });

      localPrintStream2.flush();

      System.setOut(localPrintStream1);

      return localByteArrayOutputStream.toString();
    } catch (IllegalAccessException localIllegalAccessException) {
      return "Error decompiling"; } catch (InvocationTargetException localInvocationTargetException) {
    }
    return "Error decompiling";
  }
}